﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityDataModel;
using DataAccessLayer;
using CommonUtils;

namespace BusinessLogicLayer
{
    public class WallBLL
    {
        /// <summary>     
        /// Get all messages where the username in parameter is the receiver.
        /// It also returns all status messages for the given username.
        /// </summary>
        public static List<Message> GetAllPublicMessagesForUser(String userName) {
            try
            {
                List<Message> messages = MessageDAL.GetAllPublicMessagesForUser(userName.Trim());
                foreach(Message message in messages) 
                {
                    if(MessageBLL.IsALink(message.MessageContent)) {
                        message.MessageContent = MessageBLL.TransformToHtmlLink(message.MessageContent);
                    }
                }
                return messages;
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }

        /// <summary>     
        /// Get all messages where the username or his friends are the receiver.
        /// It also returns all status messages.
        /// </summary>
        public static List<Message> GetAllPublicMessages(String userName)
        {
            try
            {
                List<Message> messages = MessageDAL.GetAllPublicMessages(userName.Trim());
                foreach (Message message in messages)
                {
                    if (MessageBLL.IsALink(message.MessageContent))
                    {
                        message.MessageContent = MessageBLL.TransformToHtmlLink(message.MessageContent);
                    }
                }
                return messages;
            }
            catch (Exception e)
            {
                System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
                LogWriter.AddLog(e, LogStatus.Error, BusinessLogicLayerSettings.Default.ApplicationName, sf.GetMethod().Name);
                throw new Exception(sf.GetMethod().Name + " unexpected error", e);
            }
        }
    }
}
